"""
基于MSSQL存储层的信号服务
"""
from typing import List, Optional
from datetime import datetime
import logging

from .mssql_storage import mssql_storage
from .schemas import SignalCollectionIn, SignalCollectionOut, DeviceInfoIn, DeviceInfoOut, SignalStatisticsOut

logger = logging.getLogger(__name__)

class MSSQLSignalService:
    """基于MSSQL存储层的4G信号采集服务"""
    
    def __init__(self):
        self.storage = mssql_storage
    
    def create_signal_collection(self, signal_data: SignalCollectionIn) -> SignalCollectionOut:
        """创建信号采集记录"""
        try:
            return self.storage.create_signal_collection(signal_data)
        except Exception as e:
            logger.error(f"创建信号采集记录失败: {e}")
            raise
    
    def get_signal_collections(
        self, 
        device_id: Optional[str] = None,
        start_time: Optional[datetime] = None,
        end_time: Optional[datetime] = None,
        limit: int = 100,
        offset: int = 0
    ) -> List[SignalCollectionOut]:
        """获取信号采集记录列表"""
        try:
            return self.storage.get_signal_collections(
                device_id=device_id,
                start_time=start_time,
                end_time=end_time,
                limit=limit,
                offset=offset
            )
        except Exception as e:
            logger.error(f"获取信号采集记录失败: {e}")
            raise
    
    def get_signal_statistics(self, device_id: str) -> Optional[SignalStatisticsOut]:
        """获取设备信号统计信息"""
        try:
            return self.storage.get_signal_statistics(device_id)
        except Exception as e:
            logger.error(f"获取信号统计信息失败: {e}")
            raise
    
    def register_device(self, device_info: DeviceInfoIn) -> DeviceInfoOut:
        """注册设备信息"""
        try:
            return self.storage.register_device(device_info)
        except Exception as e:
            logger.error(f"设备注册失败: {e}")
            raise